package com.itlong.cloud.operate.service.impl;

import com.itlong.cloud.POJO.DTO.operate.OperateProjectInfoGetPageDTO;
import com.itlong.cloud.POJO.DTO.operate.OperateUiInfoPageDTO;
import com.itlong.cloud.POJO.DTO.operate.OperateUiInfoSaveDTO;
import com.itlong.cloud.POJO.PO.UiInfoPO;
import com.itlong.cloud.POJO.PO.UiProjectInfoPO;
import com.itlong.cloud.POJO.VO.operate.OperateUiInfoGetOneVO;
import com.itlong.cloud.POJO.VO.operate.OperateUiInfoGetPageVO;
import com.itlong.cloud.POJO.VO.operate.OperateUiProjectInfoVO;
import com.itlong.cloud.PlatformConstants;
import com.itlong.cloud.operate.dao.IOperateUiManageDao;
import com.itlong.cloud.operate.service.IOperateUiManageService;
import com.itlong.cloud.utils.data.handler.SqlUtil;
import com.itlong.cloud.utils.page.Page;
import com.itlong.cloud.utils.random.LogicIdUtil;
import com.itlong.cloud.utils.words.StringHandlerUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <desc>
 *      运营-UI管理。
 * <desc/>
 *
 * @createDate 2018-03-20.
 */
@Service
public class OperateUiManageServiceImpl implements IOperateUiManageService {

    @Autowired
    IOperateUiManageDao iOperateUiManageDao;
    /**
     * <desc>
     *      保存UI信息。
     * <desc/>
     *
     * @param  saveDTO
     * @return
     * @author shengen.T
     * @createDate 2018-03-20
     */
    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    public Integer saveUiInfo(OperateUiInfoSaveDTO saveDTO) throws Exception{
        UiInfoPO uiInfoPO = new UiInfoPO();
        uiInfoPO.setUiId(LogicIdUtil.bussinessId());
        uiInfoPO.setUiName(saveDTO.getUiName());
        uiInfoPO.setUiType(saveDTO.getUiType());
        uiInfoPO.setUiUrl(saveDTO.getUiUrl());
        uiInfoPO.setUiBackground(saveDTO.getUiBackground());
        uiInfoPO.setUiLogoUrl(saveDTO.getUiLogoUrl());
        uiInfoPO.setCreateTime(new Date());
        uiInfoPO.setUpdateTime(new Date());
        uiInfoPO.setStatus(1);
        uiInfoPO.setRemark(saveDTO.getRemark());
        uiInfoPO.setShowName(saveDTO.getShowName());
        Integer result = iOperateUiManageDao.saveUiInfo(SqlUtil.durableData(uiInfoPO, PlatformConstants.TABLE_SAVE));
        //保存UI跟项目关系表
        if(result>0){
            String[] projectIdList = StringHandlerUtil.splitString(saveDTO.getProjectIds());
            if(projectIdList.length>0){
                List<UiProjectInfoPO> uiProjectInfoPOList = new ArrayList<>();
                for (int i=0;i<projectIdList.length;i++){
                    UiProjectInfoPO uiProjectInfoPO = new UiProjectInfoPO();
                    String uiId = uiInfoPO.getUiId();
                    uiProjectInfoPO.setUiId(uiId);
                    uiProjectInfoPO.setProjectId(projectIdList[i]);
                    uiProjectInfoPOList.add(uiProjectInfoPO);
                }
                iOperateUiManageDao.saveUiProjectRelation(uiProjectInfoPOList);
            }
        }
        return 1;
    }


    /**
     * <desc>
     *      更新UI信息。
     * <desc/>
     *
     * @param  updateDTO
     * @return
     * @author shengen.T
     * @createDate 2018-03-20
     */
    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    public Integer updateUiInfo(OperateUiInfoSaveDTO updateDTO) throws Exception{
        UiInfoPO uiInfoPO = new UiInfoPO();
        uiInfoPO.setUiId(updateDTO.getUiId());
        uiInfoPO.setUiName(updateDTO.getUiName());
        uiInfoPO.setUiType(updateDTO.getUiType());
        uiInfoPO.setUiUrl(updateDTO.getUiUrl());
        uiInfoPO.setUiBackground(updateDTO.getUiBackground());
        uiInfoPO.setUiLogoUrl(updateDTO.getUiLogoUrl());
        uiInfoPO.setUpdateTime(new Date());
        uiInfoPO.setRemark(updateDTO.getRemark());
        uiInfoPO.setShowName(updateDTO.getShowName());
        Integer result = iOperateUiManageDao.updateUiInfo(SqlUtil.durableData(uiInfoPO, PlatformConstants.TABLE_UPDATE));

        //保存UI跟项目关系表
        if(result>0){
            String[] projectIdList = StringHandlerUtil.splitString(updateDTO.getProjectIds());
            if(projectIdList.length>0){
                String[] uiIdList = StringHandlerUtil.splitString(updateDTO.getUiId());
                iOperateUiManageDao.delUiProjectRelation(uiIdList);
                String uiId = updateDTO.getUiId();
                List<UiProjectInfoPO> uiProjectInfoPOList = new ArrayList<>();
                for (int i=0;i<projectIdList.length;i++){
                    UiProjectInfoPO uiProjectInfoPO = new UiProjectInfoPO();
                    uiProjectInfoPO.setUiId(uiId);
                    uiProjectInfoPO.setProjectId(projectIdList[i]);
                    uiProjectInfoPOList.add(uiProjectInfoPO);
                }
                iOperateUiManageDao.saveUiProjectRelation(uiProjectInfoPOList);
            }
        }
        return 1;
    }

    /**
     * <desc>
     *      删除UI信息。
     * <desc/>
     *
     * @param  uiIds
     * @return
     * @author shengen.T
     * @createDate 2018-03-21
     */
    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    public Integer delUiInfo(String uiIds) throws Exception{
        String[] uiIdList = StringHandlerUtil.splitString(uiIds);
        Integer result = iOperateUiManageDao.updateUiInfoStatus(uiIdList);
        if(result>0){
            iOperateUiManageDao.delUiProjectRelation(uiIdList);
        }
        return 1;
    }

    /**
     * <desc>
     *      设置UI启用禁用。
     * <desc/>
     *
     * @param  uiId
     * @param  type
     * @return
     * @author shengen.T
     * @createDate 2018-03-21
     */
    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    public Integer setStatus( String uiId ,Integer type) throws Exception{
        return iOperateUiManageDao.setStatus(uiId,type);
    }


    /**
     * <desc>
     *      获取UI详细信息。
     * <desc/>
     *
     * @param  uiId
     * @return
     * @author shengen.T
     * @createDate 2018-03-22
     */
    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.NOT_SUPPORTED)
    public OperateUiInfoGetOneVO getOneUiInfo(String uiId) throws Exception{
        OperateUiInfoGetOneVO uiInfo =  iOperateUiManageDao.getOneUiInfo(uiId);
        if((uiInfo!=null)&&(!StringUtils.isBlank(uiInfo.getUiId()))){
            uiInfo.setProjectInfos(iOperateUiManageDao.getOneUiProjectInfo(uiId));
        }
        return uiInfo;
    }

    /**
     * <desc>
     *      分页获取信息。
     * <desc/>
     *
     * @param  pageDTO
     * @return
     * @author shengen.T
     * @createDate 2018-03-22
     */
    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.NOT_SUPPORTED)
    public Page<OperateUiInfoGetPageVO> getPageUiInfo(OperateUiInfoPageDTO pageDTO) throws Exception{
        //将DTO转换为Map params
        Map<String, Object> params = SqlUtil.convertBeanToMapParams(pageDTO);
        //分页类默认pageSize为20
        Page<OperateUiInfoGetPageVO> page = new Page<>();
        page.getPage().setPageSize(pageDTO.getPageSize());
        page.getPage().setCurrentPage(pageDTO.getCurrentPage());
        params.put("currentPage", page.getPage().getStartSlip());
        params.put("pageSize", page.getPage().getPageSize());
        //填充返回数据集
        page.setRows(iOperateUiManageDao.getPage(params), iOperateUiManageDao.getPageCount(params));
        return page;
    }

    /**
     * <desc>
     *      分页查询项目信息
     * <desc/>
     *
     * @param pageDTO 分页查询项目信息DTO
     * @return
     * @author shengen.T
     * @createDate 2018-03-23
     */
    public Page<OperateUiProjectInfoVO> getPageProject(OperateProjectInfoGetPageDTO pageDTO) throws Exception{
        //将DTO转换为Map params
        Map<String, Object> params = SqlUtil.convertBeanToMapParams(pageDTO);
        //分页类默认pageSize为20
        Page<OperateUiProjectInfoVO> page = new Page<>();
        page.getPage().setPageSize(pageDTO.getPageSize());
        page.getPage().setCurrentPage(pageDTO.getCurrentPage());
        params.put("currentPage", page.getPage().getStartSlip());
        params.put("pageSize", page.getPage().getPageSize());
        //填充返回数据集
        page.setRows(iOperateUiManageDao.getPageProject(params), iOperateUiManageDao.getPageCountProject(params));
        return page;
    }

}
